import 'package:base_lib/app.dart';
import 'package:base_lib/base_lib.dart';
import 'package:base_lib/common/network/loading_dialog.dart';
import 'package:base_lib/index.dart';
import 'package:common_lib/common_compoent.dart';
import 'package:doctor_app/app_component.dart';
import 'package:doctor_app/splash_page.dart';
import 'package:doctor_examine_lib/doctor_examine_componet.dart';
import 'package:doctor_inquiry_lib/doctor_inquiry_componet.dart';
import 'package:doctor_inquiry_lib/inquiry_plugin.dart';
import 'package:doctor_login_lib/component.dart';
import 'package:doctor_order_lib/component.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_lib.dart';
import 'package:doctor_user_lib/doctor_user_component.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  App.initializer(
      components, DoctorLoginComponent.loginPage, AppComponent.mainPage);
  runApp(MyApp());
}

var components = [
  AppComponent(),
  CommonComponent(),
  DoctorLoginComponent(),
  DoctorUserComponent(),
  DoctorExamineComponent(),
  DoctorPrescribeComponent(),
  DoctorOrderComponent(),
  DoctorInquiryComponent(),
];

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    MTheme mTheme = MTheme(ThemeColor.normal(), ThemeFontSize.normal());
    ThemeGlobal.instance.themeData = mTheme;

    return OverlaySupport(
      toastTheme: ToastThemeData.raw(
          textColor: Colors.white,
          background: ColorUtil.color("85000000"),
          alignment: Alignment.center),
      child: MaterialApp(
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          const FallbackCupertinoLocalisationsDelegate(),
        ],
        supportedLocales: [
          const Locale('zh', 'CH'),
          const Locale('en', 'US'),
        ],
        locale: Locale('zh'),
        navigatorKey: App.navigatorKey,
        title: '医生端',
        theme: ThemeData(
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: RootPage(),
      ),
    );
  }
}

class RootPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    LoadingDialog.init(context);
    Future.delayed(Duration(milliseconds: 100), () {
      Routers.navigateTo(context, AppComponent.splashPage);
    });
    return Scaffold(
      body: Container(
        child: Image.asset(
          "images/splash.png",
          fit: BoxFit.fill,
        ),
      ),
    );
  }
}

///fix CupertinoDialog 报错
class FallbackCupertinoLocalisationsDelegate
    extends LocalizationsDelegate<CupertinoLocalizations> {
  const FallbackCupertinoLocalisationsDelegate();

  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<CupertinoLocalizations> load(Locale locale) =>
      DefaultCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(FallbackCupertinoLocalisationsDelegate old) => false;
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title = '桃子医生端'}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            '已加载的功能模块${components.toString()}',
          ),
          RaisedButton(
            onPressed: () {
              Routers.navigateTo(context, Routers.login_page);
            },
            child: Text("登录"),
          ),
        ],
      ),
    );
  }
}
